/**
 * fileName:com.zimo.web.ctl.LoginCtl.java
 * projectName:Zimo
 */
package com.zimo.web.ctl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimo.web.entity.User;
import com.zimo.web.service.UserService;
import com.zimo.web.util.MD5;

/**
 * 用户登录控制
 * @author sg.z/2013-12-18/上午9:42:52
 */
@Controller
@RequestMapping(value="login")
public class LoginCtl extends SuperCtl{

	private Logger log = Logger.getLogger(LoginCtl.class);
	
	@Resource
	private UserService userService;
	
	/**
	 * 后台用户登录
	 *
	 * @author sg/2013-12-18/上午9:44:19
	 */
	@RequestMapping(value="/admin")
	public String adminLogin(HttpServletRequest request){
		
		return null;
	}

	/**
	 * 前端用户登录
	 *
	 * @author sg/2013-12-18/上午9:47:58
	 */
	@RequestMapping(value="/user.htm",method=RequestMethod.POST)
	public void userLogin(HttpServletRequest request,HttpServletResponse response,@RequestParam String userName,@RequestParam String userPass){
		
		userPass = MD5.md5(userPass);
		User user = userService.getUserByNameAndPass(userName, userPass);
		String status = "success";
		String msg = "登录成功";
		if(user!=null){
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			super.ajaxResponse(response, null, status,msg);
			return ;
		}
		super.ajaxResponse(response, null, "fail","用户名或密码不对");
	}
	
	@RequestMapping(value="/out.htm",method=RequestMethod.GET)
	@ResponseBody
	public void loginOut(HttpServletRequest request,HttpServletResponse response){
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user!=null){
			session.setAttribute("user", null);
			session.removeAttribute("user");
		}
		log.debug("# loginOut() 退出成功..");
		super.ajaxResponse(response, null, "success","谢谢光临");
	}
	
	
}
